<template>
  <div>
    <el-form ref="form" :model="reqParams" label-width="180px">
      <el-form-item label="手机：">
        <el-input v-model="reqParams.mobile" style="width: 70%" />
      </el-form-item>
      <el-form-item label="入职时间：">
        <el-date-picker
          v-model="reqParams.createTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="头像：">
        <!-- 组件头像 -->
        <upload-img ref="uploadImg" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateInfo">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      reqParams: {
        mobile: '',
        createTime: '',
        staffPhoto: '',
        id: this.$route.query.id
      }
    }
  },
  mounted() {
    this.hGetUserDetailById()
  },
  methods: {
    // 获取员工信息
    async hGetUserDetailById() {
      const res = await getUserDetailById(this.reqParams.id)
      this.reqParams.mobile = res.mobile
      this.reqParams.createTime = res.createTime
      // 把拿到的图片地址，赋值给组件，用来回显
      this.$refs.uploadImg.imageUrl = res.staffPhoto
    },
    // 保存信息
    updateInfo() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          // 拿到上传头像，云服务地址保存的图片
          this.reqParams.staffPhoto = this.$refs.uploadImg.imageUrl
          await saveUserDetailById(this.reqParams)
          this.$message.success('更新成功！')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
